package com.softright.net;

import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

import android.util.Log;

/**
 * 
 * @author lemon
 *
 */
public class BytesBody extends MutilPartBody {
	
	private static final String CONTENT_TYPE = "Content-Type: application/octet-stream";
	
	BytesBody(String fieldName, byte[] bodyBytes) {
		StringBuffer sb = new StringBuffer();
		sb.append("Content-Disposition: form-data; name=\"" + fieldName + "\"");
		sb.append("; filename=\"\"");
		sb.append("\r\n");
		sb.append(CONTENT_TYPE);
		sb.append("\r\n");
		sb.append("\r\n");
		ByteBuffer byteBuffer = null;
		int len = 0;
		byte[] header_bytes = sb.toString().getBytes();
		Log.d("lemon", "Image size :" + bodyBytes.length);
		len = bodyBytes.length + header_bytes.length;
		byteBuffer = ByteBuffer.allocate(len);
		byteBuffer.put(header_bytes);
		byteBuffer.put(bodyBytes);
		this.bytes = byteBuffer.array();
		length = this.bytes.length;
	}

	@Override
	public void put(DataOutputStream os) throws FileUploadException {
		try {
			os.write(bytes, 0, bytes.length);
		} catch (IOException e) {
			throw new FileUploadException(e);
		}
	}
}
